package com.southminority.ethnic.service.language.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.service.language.LanguageNationService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southminority.ethnic.mapper.language.LanguageNationMapper;
import com.southminority.ethnic.pojo.LanguageNation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ASHTR
 * @description 针对表【minority_language_culture(语言数据库-口传文化)】的数据库操作Service实现
 * @createDate 2025-08-29 00:33:49
 */
@Service
public class LanguageNationServiceImpl extends ServiceImpl<LanguageNationMapper, LanguageNation>
        implements LanguageNationService {
    @Override
    public Boolean addNation(LanguageNation languageNation) {
        if (languageNation.getFatherId()!=0){
            LanguageNation fatherNation = baseMapper.selectById(languageNation.getFatherId());
            if (fatherNation == null) {
                throw new RuntimeException("没有此父级分类");
            }
        }
        return baseMapper.insert(languageNation) > 0;
    }

    @Override
    public Boolean deleteNationById(Integer id) {
        //查新是否被引用
        LambdaQueryWrapper<LanguageNation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LanguageNation::getFatherId, id);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("此分类被引用，请先删除子分类");
        }else {
            return baseMapper.deleteById(id) > 0;
        }
    }

    @Override
    public Boolean updateNationById(Integer id, String newNationName) {
        LambdaUpdateWrapper<LanguageNation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LanguageNation::getId, id)
                .set(LanguageNation::getCategoryName, newNationName);
        Integer count = baseMapper.update(null, updateWrapper);

        if (count==0){
            throw new RuntimeException("更改对象不存在");
        }else
            return count > 0;
    }

    @Override
    public LanguageNation selectOne(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public LanguageNation selectOneByName(String name) {
        LambdaQueryWrapper<LanguageNation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LanguageNation::getCategoryName, name);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<LanguageNation> selectAll() {
//        LambdaQueryWrapper<LanguageNation> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.orderByAsc(LanguageNation::getCategoryName);
//        return baseMapper.selectList(queryWrapper);
        return baseMapper.selectList(null);
    }

    @Override
    public Page<LanguageNation> selectOnesPageKey(Integer pageNum, Integer pageSize, String key) {
        Page<LanguageNation> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<LanguageNation> queryWrapper = new LambdaQueryWrapper<>();
        if (key != null && !key.isEmpty()){
            queryWrapper.like(LanguageNation::getCategoryName, key);
        }
        queryWrapper.orderByAsc(LanguageNation::getId);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<LanguageNation> selectRootAll() {
        LambdaQueryWrapper<LanguageNation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LanguageNation::getFatherId, 0);
        return baseMapper.selectList(queryWrapper);
    }

}
